//
//  TestCollectionViewPageFlowLayout.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2020/10/6.
//  Copyright © 2020 moon. All rights reserved.
//

#import "TestCollectionViewPageFlowLayout.h"
#import "Menu.h"
#import "PageFlowMenuTableViewCell_OnePage.h"
#import "PageFlowMenuTableViewCell_OneHalfPage.h"
#import "PageFlowMenuTableViewCell_Picker.h"
#import "PageFlowMenuTableViewCell_SizeFitsPage.h"
#import "PageFlowMenuTableViewCell_One2Page.h"
#import "LedMenuTalbeViewCell.h"
@interface TestCollectionViewPageFlowLayout ()
@property(nonatomic) BOOL shouldReuse;
@end

@implementation TestCollectionViewPageFlowLayout
+ (MKTFuncItem *)funcItem{
    return [MKTFuncItem funcWithName:@"PageFlow集合" viewControllerClass:self];
}
- (void)viewDidLoad{
    [super viewDidLoad];
    [self __reloadData];
    
    UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(__onRefresh:)];
    
    UISwitch *reuseSwitch = [[UISwitch alloc] init];
    reuseSwitch.on = self.shouldReuse;
    [reuseSwitch addTarget:self action:@selector(__onReuseChange:) forControlEvents:UIControlEventValueChanged];
    UIBarButtonItem *resueItem = [[UIBarButtonItem alloc] initWithCustomView:reuseSwitch];
    
    self.navigationItem.rightBarButtonItems = @[refreshItem,resueItem];
}
- (void)__onReuseChange:(UISwitch *)sender{
    self.shouldReuse = sender.on;
    [self __reloadData];
}
- (void)__onRefresh:(id)sender{
//    [self __reloadData];
    [self.model reloadTableViewData];
}
- (void)__reloadData{
    self.model.reuseCell = self.shouldReuse;
    [self.model removeAllSectionModels];
    
    {
        MKUITableViewCellModel *cm = [MKUITableViewCellModel modelWithValue:[Menu sharedMenusWithCount:20] cellClass:[LedMenuTalbeViewCell class]];
        [self.model addCellModel:cm];
    }
    {
        MKUITableViewCellModel *cm = [MKUITableViewCellModel modelWithValue:[Menu sharedMenusWithCount:1] cellClass:[LedMenuTalbeViewCell class]];
        [self.model addCellModel:cm];
    }

    {
        MKUITableViewCellModel *cm = [MKUITableViewCellModel modelWithValue:[Menu sharedMenusWithCount:2] cellClass:[LedMenuTalbeViewCell class]];
        cm[@"autoScroll"] = @(NO);
        [self.model addCellModel:cm];
    }
    {
        MKUITableViewCellModel *cm = [MKUITableViewCellModel modelWithValue:[Menu sharedMenusWithCount:2] cellClass:[LedMenuTalbeViewCell class]];
        [self.model addCellModel:cm];
    }

    [self.model addCellModel:[self onePageElement]];
    [self.model addCellModel:[self onePage3Element]];
//    for (int i=0; i<[Menu sharedMenus].count; i++) {
//        MKUITableViewCellModel *cm = [self oneHalfPageElement];
//        cm[@"indexPathAtPagingCell"] = [NSIndexPath indexPathForRow:i inSection:0];
//        [self.model addCellModel:cm];
//    }

    [self.model addCellModel:[self oneHalfPageElement]];
    
    [self.model addCellModel:[self one2PageElement]];
//
    [self.model addCellModel:[self oneHalfSyncLoadPageElement]];
    [self.model addCellModel:[self oneHalfLargePageElement]];

    [self.model addCellModel:[self pickerPageElement]];
    [self.model addCellModel:[self picker10PageElement]];
    [self.model addCellModel:[self pickerMaxPageElement]];

    [self.model addCellModel:[self sizeFitsMinPageElement]];
    [self.model addCellModel:[self sizeFitsPageElement]];
    [self.model addCellModel:[self sizeFitsMaxPageElement]];

    [self.model reloadTableViewData];
}
- (BOOL)reuseIdentityCell{//是否重用cell。当cell数量少很复杂时，可以不重用cell，以防止滚动卡顿
    return self.shouldReuse;
}
- (void)setupCellModel:(MKUITableViewCellModel *)cm{

}
- (MKUITableViewCellModel *)onePageElement{
    NSArray<Menu *> *menus = [Menu sharedMenus];
    MKUITableViewCellModel *cm = [MKUITableViewCellModel modelWithValue:menus];
    cm[@"shouldReuse"] = @(self.shouldReuse);
    cm.cellClass = [PageFlowMenuTableViewCell_OnePage class];
    cm[@"autoScrollDistance"] = @(1);
    cm[@"autoScroll"] = @(NO);
    cm[@"pagingEnabled"] = @(NO);
    [self setupCellModel:cm];
    return cm;
}
- (MKUITableViewCellModel *)onePage3Element{
    NSArray<Menu *> *menus = [Menu sharedMenusWithCount:15];
    MKUITableViewCellModel *cm = [MKUITableViewCellModel modelWithValue:menus];
    cm[@"shouldReuse"] = @(self.shouldReuse);
    cm.cellClass = [PageFlowMenuTableViewCell_OnePage class];
    cm[@"autoScrollDistance"] = @(1);
    cm[@"step"] = @(5);
    cm[@"decelerationRate"] = @(UIScrollViewDecelerationRateFast);
    [self setupCellModel:cm];
    return cm;
}
- (MKUITableViewCellModel *)oneHalfPageElement{
    NSArray<Menu *> *menus = [Menu sharedMenus];
    MKUITableViewCellModel *cm = [MKUITableViewCellModel modelWithValue:menus];
    cm[@"shouldReuse"] = @(self.shouldReuse);
    cm[@"autoScrollDistance"] = @(-1);
    cm.cellClass = [PageFlowMenuTableViewCell_OneHalfPage class];
    [self setupCellModel:cm];
    return cm;
}
- (MKUITableViewCellModel *)oneHalfSyncLoadPageElement{
    NSArray<Menu *> *menus = [Menu sharedUrlMenus];
    MKUITableViewCellModel *cm = [MKUITableViewCellModel modelWithValue:menus];
    cm[@"shouldReuse"] = @(self.shouldReuse);
    cm.cellClass = [PageFlowMenuTableViewCell_OneHalfPage class];
    [self setupCellModel:cm];
    return cm;
}
- (MKUITableViewCellModel *)oneHalfLargePageElement{
    NSArray<Menu *> *menus = [Menu sharedMenusWithCount:15000];
    MKUITableViewCellModel *cm = [MKUITableViewCellModel modelWithValue:menus];
    cm[@"shouldReuse"] = @(self.shouldReuse);
    cm.cellClass = [PageFlowMenuTableViewCell_OneHalfPage class];
    [self setupCellModel:cm];
    return cm;
}
- (MKUITableViewCellModel *)pickerPageElement{
    NSArray<Menu *> *menus = [Menu sharedMenusWithCount:60];
    MKUITableViewCellModel *cm = [MKUITableViewCellModel modelWithValue:menus];
    cm[@"shouldReuse"] = @(self.shouldReuse);
    cm[@"autoScrollDistance"] = @(2);
    cm[@"itemAlignment"] = @(MKCGRectAlignmentMin);
    cm.cellClass = [PageFlowMenuTableViewCell_Picker class];
    [self setupCellModel:cm];
    return cm;
}
- (MKUITableViewCellModel *)pickerMaxPageElement{
    NSArray<Menu *> *menus = [Menu sharedMenusWithCount:11];
    MKUITableViewCellModel *cm = [MKUITableViewCellModel modelWithValue:menus];
    cm[@"shouldReuse"] = @(self.shouldReuse);
    cm[@"itemAlignment"] = @(MKCGRectAlignmentMax);
    cm.cellClass = [PageFlowMenuTableViewCell_Picker class];
    [self setupCellModel:cm];
    return cm;
}
- (MKUITableViewCellModel *)picker10PageElement{
    NSArray<Menu *> *menus = [Menu sharedMenusWithCount:10];
    MKUITableViewCellModel *cm = [MKUITableViewCellModel modelWithValue:menus];
    cm[@"shouldReuse"] = @(self.shouldReuse);
    cm[@"itemAlignment"] = @(MKCGRectAlignmentMid);
    cm.cellClass = [PageFlowMenuTableViewCell_Picker class];
    [self setupCellModel:cm];
    return cm;
}
- (MKUITableViewCellModel *)sizeFitsPageElement{
    NSArray<Menu *> *menus = [Menu sharedMenus];
    MKUITableViewCellModel *cm = [MKUITableViewCellModel modelWithValue:menus];
    cm[@"shouldReuse"] = @(self.shouldReuse);
    cm.cellClass = [PageFlowMenuTableViewCell_SizeFitsPage class];
    [self setupCellModel:cm];
    return cm;
}
- (MKUITableViewCellModel *)sizeFitsMinPageElement{
    NSArray<Menu *> *menus = [Menu sharedMenus];
    MKUITableViewCellModel *cm = [MKUITableViewCellModel modelWithValue:menus];
    cm.reuseIdentity = NSStringFromSelector(_cmd);
    cm[@"shouldReuse"] = @(self.shouldReuse);
    cm.cellClass = [PageFlowMenuTableViewCell_SizeFitsPage class];
    [self setupCellModel:cm];
    return cm;
}
- (MKUITableViewCellModel *)sizeFitsMaxPageElement{
    NSArray<Menu *> *menus = [Menu sharedMenus];
    MKUITableViewCellModel *cm = [MKUITableViewCellModel modelWithValue:menus];
    cm[@"shouldReuse"] = @(self.shouldReuse);
    cm[@"itemAlignment"] = @(MKCGRectAlignmentMax);
    cm.cellClass = [PageFlowMenuTableViewCell_SizeFitsPage class];
    [self setupCellModel:cm];
    return cm;
}
- (MKUITableViewCellModel *)one2PageElement{
    NSArray<Menu *> *menus = [Menu sharedMenus];
    MKUITableViewCellModel *cm = [MKUITableViewCellModel modelWithValue:menus];
    cm[@"shouldReuse"] = @(self.shouldReuse);
    cm.cellClass = [PageFlowMenuTableViewCell_One2Page class];
    [self setupCellModel:cm];
    return cm;
}

@end
